package com.a214.yxz.yuejian013.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.a214.yxz.yuejian013.Base.BaseSActivity;
import com.a214.yxz.yuejian013.Common.utilMeth;
import com.a214.yxz.yuejian013.JavaBean.StringBean;
import com.a214.yxz.yuejian013.Listener.UserListener;
import com.a214.yxz.yuejian013.Model.UserModel;
import com.a214.yxz.yuejian013.R;
public class LoginActivity extends BaseSActivity implements utilMeth,View.OnClickListener,UserListener {

    private Button login;
    private Button regUser;
    private Button loginUser;
    public EditText username;
    public EditText password;

    String uname;
    String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        inEvents();
        login.setOnClickListener(this);
    }

    public void initViews() {
        regUser = (Button) findViewById(R.id.regUser);
        loginUser = (Button) findViewById(R.id.login);
        username = (EditText) findViewById(R.id.user);
        password = (EditText) findViewById(R.id.password);
        login= (Button) findViewById(R.id.login);
    }

    public void inEvents() {
        regUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                uname=username.getText().toString();
                pwd=password.getText().toString();
                UserModel userModel=new UserModel();
                userModel.getLogin(uname,pwd,this);
                break;
        }
    }

    @Override
    public void onResponse(StringBean stringBean) {

        Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
        Intent intent=new Intent(LoginActivity.this, MainActivity.class);
        SharedPreferences sharedPreferences = getSharedPreferences("user_info",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("user_id",stringBean.getId());
        editor.commit();
        startActivity(intent);

    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, ""+msg, Toast.LENGTH_SHORT).show();
    }


}
